package org.spring.blog.controller.admin;

import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.spring.blog.pojo.FriendLink;
import org.spring.blog.response.ResponseResult;
import org.spring.blog.response.ResponseState;
import org.spring.blog.service.IFriendLinkService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@PreAuthorize("@permission.admin()")
@RestController
@RequestMapping("/admin/friend_link")
public class FriendLinkAdminApi {

    private final IFriendLinkService friendLinkService;

    public FriendLinkAdminApi(IFriendLinkService friendLinkService) {
        this.friendLinkService = friendLinkService;
    }


    /**
     * 添加友情链接
     *
     * @param _friendLink 友情链接对象
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.friendLink_url_cant_empty_code, message = ResponseState.friendLink_url_cant_empty_message),
            @ApiResponse(code = ResponseState.friendLink_name_cant_empty_code, message = ResponseState.friendLink_name_cant_empty_message),
            @ApiResponse(code = ResponseState.friendLink_logo_cant_empty_code, message = ResponseState.friendLink_logo_cant_empty_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PostMapping
    public ResponseResult insertFriend(@RequestBody FriendLink _friendLink) {
        return friendLinkService.insertFriend(_friendLink);
    }

    /**
     * 删除友情链接
     *
     * @param _Id 链接id
     * @return 自定义返回对象
     */
    @ApiResponses(@ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message))
    @DeleteMapping("/{id}")
    public ResponseResult delete(@PathVariable("id") String _Id) {
        return friendLinkService.delete(_Id);
    }

    /**
     * 查询友情链接
     *
     * @param _Id 对象id
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.friendLink_not_have_code, message = ResponseState.friendLink_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @GetMapping("/{id}")
    public ResponseResult query(@PathVariable("id") String _Id) {
        return friendLinkService.query(_Id);
    }

    /**
     * 更新友情链接
     *
     * @param _Id 链接id
     * @return 自定义返回对象
     */
    @ApiResponses({
            @ApiResponse(code = ResponseState.no_power_code, message = ResponseState.no_power_message),
            @ApiResponse(code = ResponseState.friendLink_not_have_code, message = ResponseState.friendLink_not_have_message),
            @ApiResponse(code = ResponseState.success_code, message = ResponseState.success_message),
    })
    @PutMapping("/{id}")
    public ResponseResult update(@PathVariable("id") String _Id,
                                 @RequestBody FriendLink _friendLink) {
        return friendLinkService.update(_Id, _friendLink);
    }

    /**
     * 获取友情链接列表
     *
     * @return 自定义返回对象
     */
    @GetMapping("/list")
    public ResponseResult list() {
        return friendLinkService.listForAdmin();
    }
}
